<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/userManage/userMisc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/init/acc_xml_builder.php';

function dorpc_load_acc_data($data, &$res) {
    switch($data["descriptor"]) {
    case "saleman":
        $res["data"] = get_saleman_source_json();
        set_res_OK($res);
        break;
    case "accounts_for_tree":
        $res["data"] = get_accounts_tree_json();
        set_res_OK($res);
        break;
    case "acc_new_voch":
        $res["data"] = get_acc_source_json();
        set_res_OK($res);
        break;
    case "current":
        $res["data"] = get_current_source_json();
        set_res_OK($res);
        break;
    case "project":
        $res["data"] = get_prj_source_json();
        set_res_OK($res);
        break;
    default:
        set_res_NG($res, null, "unkown descriptor.");
        break;
    }
}

function get_saleman_source_json() { //取得业务员列表
    $pdb = new acc_DB();
    $rst = $pdb->q("select * from users where role_id='10006' order by loginId;");
    $retArr = array();
    $chkArr = array(); //重名Check
    
    while($r = $rst->fetchArray(SQLITE3_ASSOC)) {
        $loginId = $r["loginId"];
        $uName = $r["uName"];

        $showName = $uName;
        if(in_array($uName, $chkArr)) {
            $showName = $uName . "(" . $loginId . ")"; //重名时,附加年龄识别 或许好些
        } else {
            array_push($chkArr, $uName);           
        }

        $o["label"] = $showName;
        $o["value"] = $showName;
        $o["abbr"] = $loginId;
        $o["v"] = $r["id"];
        array_push($retArr, $o); //有歧义时可强转:(object)$o, (array)$o
    }
    $pdb->d();
    return json_encode($retArr);    
// 通常索引数组 经过json_encode → JSON.parse后被解释为 js.array 　例Array('one', 'two') ⇒　["one","two"];
// 通常关联数组 经过json_encode → JSON.parse后被解释为 js.object　例Array('1'=>'one', '2'=>'two')⇒　{"1":"one","2":"two"} 
}

function getItemById_accounts_tree(&$accMem, $id) {
    for($i = 0; $i < count($accMem); $i++) {
        if($id == $accMem[$i]["id"]) {
            return $accMem[$i];
        }
    }
    return null;
}
function make1_item_accounts_tree(&$accMem, $id, &$outObj) {
    $me = getItemById_accounts_tree($accMem, $id);
    if(empty($me)) {
        //error route
        return;
    }
    $outObj["id"] = $me["id"];
    $outObj["text"] = $me["serialNo"] . "-" . $me["name"];

    foreach ($me as $key => $value) {
        $outObj["attributes"]["account__" . $key] = $value; //tabName__filedName
    }

    $outObj["children"] = array();
    for($i = 0; $i < count($accMem); $i++) {
        if($id == $accMem[$i]["parentId"]) {
            $child;
            make1_item_accounts_tree($accMem, $accMem[$i]["id"], $child);
            array_push($outObj["children"], $child);
        }
    }
}

function get_accounts_tree_json() {
    $accMem = null;
    getQueryResultAsArray("select * from account;",  $accMem);
    $root;
    make1_item_accounts_tree($accMem, 1, $root);
    $ret = array();
    array_push($ret, $root);
    return json_encode($ret);
}

//-----------------------------------------------------------------
function getCategory($accRecord) {
    $firstNum = "";
    if(preg_match('/(\d)(\d*)/', $accRecord["serialNo"], $matchRes1)) { //取出会计科目的首位
        $firstNum = $matchRes1[1];
    }
    switch ($firstNum) {
        case "1":
            return "资产";
        case "2":
            return "负债";
        case "3":
            return "权益";
        case "4":
            return "成本";
        case "5":
            return "损益";
        default:
            return "未分类";
    }
    return "N/A";
}

function getAccItemById(&$accArr, $id) {
    for($i = 0; $i < count($accArr); ++$i) {
        if($id == $accArr[$i]["id"]) {
            return $accArr[$i];
        }
    }
    return null;
}

function get_acc_showName(&$accArr, $accId) {
    $it = getAccItemById($accArr, $accId);
    $ret = $it["name"];
    $parentId = $it["parentId"];
    while(1 != $parentId) { //ROOT_ACCOUT.id=1   
        $it = getAccItemById($accArr, $parentId);
        $ret = $it["name"] . "-" . $ret;
        $parentId = $it["parentId"];
    }
    return $ret;
}

function get_acc_source_json() {
    $accArr = null;
    $ret = array();
    getQueryResultAsArray("select * from account;",  $accArr);
    for($i = 0; $i < count($accArr); ++$i) {
        $id = $accArr[$i]["id"];
        if(0 == is_end_acc($id)) { //非叶子科目略过
            continue;
        }

        $showName = get_acc_showName($accArr, $id);
        $cate = getCategory($accArr[$i]);

        $o["label"] = $accArr[$i]["serialNo"] . "," . $showName;
        $o["category"] = $cate;
        $o["v"] = $id;
        array_push($ret, $o);
    }
    return json_encode($ret);
}

//-------------------------------------------------------------
function get_prj_source_json() { //取得项目列表
    $Arr = null;
    $ret = array();
    getQueryResultAsArray("select * from prjects;",  $Arr);
    for($i = 0; $i < count($Arr); ++$i) {
        $o["label"] = $Arr[$i]["name"];
        $o["abbr"] = $Arr[$i]["abbr"];
        $o["v"] = $Arr[$i]["id"];
        array_push($ret, $o);
    }
    return json_encode($ret);
}

function get_current_source_json() { //取得往来列表
    $Arr = null;
    $ret = array();
    getQueryResultAsArray("select * from companys;",  $Arr);
    for($i = 0; $i < count($Arr); ++$i) {
        $o["label"] = $Arr[$i]["name"];
        $o["abbr"] = $Arr[$i]["abbr"];
        $o["v"] = $Arr[$i]["id"];
        $o["cus"] = $Arr[$i]["isCustomer"];
        $o["sup"] = $Arr[$i]["isSupplier"];
        array_push($ret, $o);
    }
    return json_encode($ret);
}

?>